package cn.fl.capital.service.impl;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapPrjArInfoQC;
import cn.fl.capital.constant.CapPrjArInfoConstant;
import cn.fl.capital.dto.CapPrjArInfoDTO;
import cn.fl.capital.service.ICapPrjArInfoApiService;
import cn.fl.capital.service.ICapPrjArInfoService;
import cn.fl.capital.transform.CapPrjArInfoVOTransform;
import cn.fl.capital.vo.CapPrjArInfoTableVO;
import cn.fl.capital.vo.CapPrjArInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjCstBscInfoQueryCondition;
import cn.fl.project.condition.PrjPrdBscInfoQueryCondition;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.transform.PrjCstBscInfoVOTransform;
import cn.fl.project.transform.PrjPrdBscInfoVOTransform;
import cn.fl.project.transform.PrjPrdDtlVOTransform;
import cn.fl.project.vo.PrjCstBscInfoVO;
import cn.fl.project.vo.PrjPrdBscInfoVO;
import cn.fl.project.vo.PrjPrdDtlVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.mongo.facade.capital.api.ICapPriArInfoMongoService;
import fl.mongo.facade.capital.dto.CapPriArInfoDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 信审项目剩余敞口信息 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CapPrjArInfoServiceImpl implements ICapPrjArInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ICapPrjArInfoService.class);

	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private ICapPrjArInfoApiService capPrjArInfoApiService;


	@Autowired
	private IProjectApiService projectApiService;

	@Autowired
	private ICapPriArInfoMongoService capPriArInfoMongoService;





	@Override
	public CapPrjArInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		CapPrjArInfoDTO dto = null;//this.自定义ApiService.selectById(id);
		return CapPrjArInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapPrjArInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.capPrjArInfoApiService.add(CapPrjArInfoVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.capPrjArInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(CapPrjArInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.capPrjArInfoApiService.modify(CapPrjArInfoVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.capPrjArInfoApiService.searchListPage(page);
		//TODO 业务逻辑
		return null;
	}

	/**
	 * 根据项目ID获取敞口统计信息
	 * @param condition
	 * @param isMongdb 数据源 1mongodb 2数据库
	 * @return
	 */
	@Override
	public List<CapPrjArInfoVO> searchList(CapPrjArInfoQC condition, boolean isMongodb) throws BaseException {
		List<CapPrjArInfoVO> resultList = new ArrayList<CapPrjArInfoVO>();
		if(isMongodb){
			String buOrgCd = condition.getBuOrgCd();
			Long prjId = condition.getPrjId();
			Date date = new Date();
			//承租人ID
			List<Long> czCstIdList = new ArrayList<Long>();
			//担保人ID
			List<Long> dbCstIdList = new ArrayList<Long>();
			//挂靠公司ID
			List<Long> lnkCompIdList = new ArrayList<Long>();
			//供应商ID
			List<Long> splIdList = new ArrayList<Long>();
			/**
			 * 1.prjId--> PRJ_CST_BSC_INFO 承租人CST_RL_CD 1
			 * 2.prjId--> PRJ_CST_BSC_INFO 担保人CST_RL_CD 2
			 * 3.prjId--> PRJ_PRD_DTL 挂靠公司
			 * 4.prjId--> PRJ_PRD_BSC_INFO 供应商
			 */
			//按类型分别查询 承租人 担人 挂靠公司 供应商
			//承租人 担保人信息
			PrjCstBscInfoQueryCondition cstBscInfoQc = new PrjCstBscInfoQueryCondition();
			cstBscInfoQc.setPrjId(prjId);
			List<PrjCstBscInfoVO> prjCstBscInfoVOList = PrjCstBscInfoVOTransform.toVOList(projectApiService.searchPrjCstBscInfoList(cstBscInfoQc,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd()));
			BaseLogUtils.info(logger,"getMainTypeAndIds","查询项目客户基本信息信息-->承租人,担保人,"+ JSON.toJSONString(prjCstBscInfoVOList));

			//供应商信息
			PrjPrdBscInfoQueryCondition prjPrdBscInfoQc = new PrjPrdBscInfoQueryCondition();
			prjPrdBscInfoQc.setPrjId(prjId);
			List<PrjPrdBscInfoVO> prjPrdBscInfoVOList = PrjPrdBscInfoVOTransform.toVOList(this.projectApiService.searchPrjPrdList(prjPrdBscInfoQc,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd()));
			BaseLogUtils.info(logger,"getMainTypeAndIds","查询项目产品基本信息信息-->供应商,"+ JSON.toJSONString(prjPrdBscInfoVOList));

			//挂靠公司
			PrjPrdDtlQueryCondition prjPrdDtlQc = new PrjPrdDtlQueryCondition();
			prjPrdDtlQc.setPrjId(prjId);
			prjPrdDtlQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			prjPrdDtlQc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			List<PrjPrdDtlVO> prjPrdDtlVOList = PrjPrdDtlVOTransform.toVOList(projectApiService.selectPrjPrdDtlList(prjPrdDtlQc));
			BaseLogUtils.info(logger,"getMainTypeAndIds","查询项目产品明细信息-->挂靠公司,"+ JSON.toJSONString(prjPrdDtlVOList));

			//拼装主体类型ID LIST
			for(PrjCstBscInfoVO prjCstBscInfoVO : prjCstBscInfoVOList){
				if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(prjCstBscInfoVO.getCstRlCd())){
					//承租人
					if(!czCstIdList.contains(prjCstBscInfoVO.getCstId())){
						czCstIdList.add(prjCstBscInfoVO.getCstId());
					}
				}else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2.equals(prjCstBscInfoVO.getCstRlCd())){
					//担保人
					if(!dbCstIdList.contains(prjCstBscInfoVO.getCstId())){
						dbCstIdList.add(prjCstBscInfoVO.getCstId());
					}
				}
			}

			//供应商
			for(PrjPrdBscInfoVO prjPrdBscInfoVO : prjPrdBscInfoVOList){
				if(null != prjPrdBscInfoVO.getSplId()){
					if(!splIdList.contains(prjPrdBscInfoVO.getSplId())){
						splIdList.add(prjPrdBscInfoVO.getSplId());
					}
				}
			}

			//挂靠公司
			for(PrjPrdDtlVO prjPrdDtlVO : prjPrdDtlVOList){
				if(null != prjPrdDtlVO.getLnkCompId()){
					if(!lnkCompIdList.contains(prjPrdDtlVO.getLnkCompId())){
						lnkCompIdList.add(prjPrdDtlVO.getLnkCompId());
					}
				}
			}
			List<fl.mongo.facade.capital.dto.CapPriArInfoDTO> resultMongoDataList = new ArrayList<fl.mongo.facade.capital.dto.CapPriArInfoDTO>();
			//承租人
			if(CollectionUtils.isNotEmpty(czCstIdList)){
				List<fl.mongo.facade.capital.dto.CapPriArInfoDTO> czCstMongoDataList = capPriArInfoMongoService.getByListCondition(czCstIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1,date,buOrgCd);
				if(CollectionUtils.isNotEmpty(czCstMongoDataList)){
					resultMongoDataList.addAll(czCstMongoDataList);
					BaseLogUtils.info(logger, "searchList", "承租人敞口信息集合:" + JSONObject.toJSONString(czCstMongoDataList), CurrentThreadContext.getCurrentUserName());
				}else{
					//添加承租人信息
					for(PrjCstBscInfoVO prjCstBscInfoVO : prjCstBscInfoVOList){
						if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(prjCstBscInfoVO.getCstRlCd())){
							fl.mongo.facade.capital.dto.CapPriArInfoDTO dto
									= this.getMongoCapPriArInfoDTO(prjId,prjCstBscInfoVO.getCstId(),prjCstBscInfoVO.getCstNm(),CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1);
							resultMongoDataList.add(dto);
						}
					}
				}
			}

			//担保人
			if(CollectionUtils.isNotEmpty(dbCstIdList)){
				List<fl.mongo.facade.capital.dto.CapPriArInfoDTO> dbCstMongoDataList = capPriArInfoMongoService.getByListCondition(dbCstIdList,CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1,date,buOrgCd);
				if(CollectionUtils.isNotEmpty(dbCstMongoDataList)){
					for(fl.mongo.facade.capital.dto.CapPriArInfoDTO dto : dbCstMongoDataList){
						dto.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2);
					}
					resultMongoDataList.addAll(dbCstMongoDataList);
					BaseLogUtils.info(logger, "searchList", "担保人敞口信息集合:" + JSONObject.toJSONString(dbCstMongoDataList), CurrentThreadContext.getCurrentUserName());

				}else{
					//添加担保人信息
					for(PrjCstBscInfoVO prjCstBscInfoVO : prjCstBscInfoVOList){
						if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2.equals(prjCstBscInfoVO.getCstRlCd())){
							fl.mongo.facade.capital.dto.CapPriArInfoDTO dto
									= this.getMongoCapPriArInfoDTO(prjId,prjCstBscInfoVO.getCstId(),prjCstBscInfoVO.getCstNm(),CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2);
							resultMongoDataList.add(dto);
						}
					}
				}
			}

			//挂靠公司
			if(CollectionUtils.isNotEmpty(lnkCompIdList)){
				List<fl.mongo.facade.capital.dto.CapPriArInfoDTO> lnkCompMongoDataList = capPriArInfoMongoService.getByListCondition(lnkCompIdList,CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3,date,buOrgCd);
				if(CollectionUtils.isNotEmpty(lnkCompMongoDataList)){
					resultMongoDataList.addAll(lnkCompMongoDataList);
					BaseLogUtils.info(logger, "searchList", "挂靠公司敞口信息集合:" + JSONObject.toJSONString(lnkCompMongoDataList), CurrentThreadContext.getCurrentUserName());

				}else{
					//添加挂靠公司信息
					for(PrjPrdDtlVO prjPrdDtlVO : prjPrdDtlVOList){
						fl.mongo.facade.capital.dto.CapPriArInfoDTO dto
								= this.getMongoCapPriArInfoDTO(prjId,prjPrdDtlVO.getLnkCompId(),prjPrdDtlVO.getLnkCompNm(),CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3);
						resultMongoDataList.add(dto);
					}
				}
			}

			//供应商
			if(CollectionUtils.isNotEmpty(splIdList)){
				List<fl.mongo.facade.capital.dto.CapPriArInfoDTO> splMongoDataList = capPriArInfoMongoService.getByListCondition(splIdList,CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4,date,buOrgCd);
				if(CollectionUtils.isNotEmpty(splMongoDataList)){
					resultMongoDataList.addAll(splMongoDataList);
					BaseLogUtils.info(logger, "searchList", "供应商敞口信息集合:" + JSONObject.toJSONString(splMongoDataList), CurrentThreadContext.getCurrentUserName());

				}else{
					//添加供应商信息
					for(PrjPrdBscInfoVO prjPrdBscInfoVO : prjPrdBscInfoVOList){
						fl.mongo.facade.capital.dto.CapPriArInfoDTO dto
								= this.getMongoCapPriArInfoDTO(prjId,prjPrdBscInfoVO.getSplId(),prjPrdBscInfoVO.getSplNm(),CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4);
						resultMongoDataList.add(dto);
					}
				}
			}
			resultList = CapPrjArInfoVOTransform.mongoDtoVOList(resultMongoDataList);
		}else {
			List<CapPrjArInfoDTO> dtoList = this.capPrjArInfoApiService.searchList(condition);
			resultList  = CapPrjArInfoVOTransform.toVOList(dtoList);
		}
		if(CollectionUtils.isEmpty(resultList)){
			//初始化数据表格
			BaseLogUtils.info(logger, "searchList", "敞口信息不存在,初始化数据！");
			CapPrjArInfoVO cst = new CapPrjArInfoVO();
			cst.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1);
			CapPrjArInfoVO db = new CapPrjArInfoVO();
			db.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2);
			CapPrjArInfoVO lnkComp = new CapPrjArInfoVO();
			lnkComp.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3);
			CapPrjArInfoVO spl = new CapPrjArInfoVO();
			spl.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4);
			resultList.add(cst);
			resultList.add(db);
			resultList.add(lnkComp);
			resultList.add(spl);
		}else{
			Set<String> set = new HashSet<String>();
			for(CapPrjArInfoVO vo : resultList){
				set.add(vo.getMainTypCd());
			}
			if(!set.contains(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1)) {
				CapPrjArInfoVO cst = new CapPrjArInfoVO();
				cst.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1);
				resultList.add(cst);
			}
			if(!set.contains(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2)) {
				CapPrjArInfoVO db = new CapPrjArInfoVO();
				db.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2);
				resultList.add(db);
			}
			if(!set.contains(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3)){
				CapPrjArInfoVO lnkComp = new CapPrjArInfoVO();
				lnkComp.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3);
				resultList.add(lnkComp);
			}
			if(!set.contains(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4)){
				CapPrjArInfoVO spl = new CapPrjArInfoVO();
				spl.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4);
				resultList.add(spl);
			}

			//集合重排序
			Collections.sort(resultList, new Comparator<CapPrjArInfoVO>() {
				@Override
				public int compare(CapPrjArInfoVO o1, CapPrjArInfoVO o2) {
					return Integer.valueOf(o1.getMainTypCd()).compareTo(Integer.valueOf(o2.getMainTypCd()));
				}
			});
		}
		resultList = resolveCapPrjArInfo(resultList);
		return resultList;


	}

	@Override
	public Map<String, List<Long>> getMainTypeAndIds(Long prjId) throws BaseException {
		Map<String,List<Long>> mainTypeAndIdMap = new HashMap<String,List<Long>>();
		//承租人ID
		List<Long> czCstIdList = getCzCstIdList(prjId);
		//担保人ID
		List<Long> dbCstIdList = getDbCstIdList(prjId);
		//挂靠公司ID
		List<Long> lnkCompIdList = getLnkCompIdList(prjId);
		//供应商ID
		List<Long> splIdList = getSplIdList(prjId);

		mainTypeAndIdMap.put("mainType1",czCstIdList);
		mainTypeAndIdMap.put("mainType2",dbCstIdList);
		mainTypeAndIdMap.put("mainType3",lnkCompIdList);
		mainTypeAndIdMap.put("mainType4",splIdList);
		return mainTypeAndIdMap;

	}




	/**
	 * 需要将一条敞口信息记录分解为五条用于前端展示
	 * @param vo
	 * @return
	 */
	public  List<CapPrjArInfoVO>  resolveCapPrjArInfo(List<CapPrjArInfoVO> voList){
		List<CapPrjArInfoVO> list = new ArrayList<CapPrjArInfoVO>();
		for(CapPrjArInfoVO object : voList){
			//融资新车
			CapPrjArInfoVO rzNewCar = new CapPrjArInfoVO();
			rzNewCar.setRecordType(0);
			rzNewCar.setRzOrDb(0);
			rzNewCar.setCarType(1);
			rzNewCar.setId(object.getId());
			rzNewCar.setCrtUsrId(object.getCrtUsrId());
			rzNewCar.setPrjId(object.getPrjId());//项目ID
			rzNewCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
			rzNewCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
			rzNewCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
			rzNewCar.setFlNewOvdHisCnt(object.getFlNewOvdHisCnt());//融资新车-历史30+逾期次数
			rzNewCar.setFlNewOvdCnt(object.getFlNewOvdCnt());//融资新车-当前逾期支付表数
			rzNewCar.setFlNewPrjAudAmt(object.getFlNewPrjAudAmt());//融资新车-已发起项目评审未通过的支付表敞口
			rzNewCar.setFlNewPayAmt(object.getFlNewPayAmt());//融资新车-未放款支付表敞口
			rzNewCar.setFlNewPaidAmt(object.getFlNewPaidAmt());//融资新车-已放款支付表敞口

			//融资二手车
			CapPrjArInfoVO rzOldCar = new CapPrjArInfoVO();
			rzOldCar.setRecordType(0);
			rzOldCar.setRzOrDb(0);
			rzOldCar.setCarType(0);
			rzOldCar.setId(object.getId());
			rzOldCar.setCrtUsrId(object.getCrtUsrId());
			rzOldCar.setPrjId(object.getPrjId());//项目ID
			rzOldCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
			rzOldCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
			rzOldCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
			rzOldCar.setFlUsedOvdHisCnt(object.getFlUsedOvdHisCnt());//融资二手车-历史30+逾期次数
			rzOldCar.setFlUsedOvdCnt(object.getFlUsedOvdCnt());//融资二手车-当前逾期支付表数
			rzOldCar.setFlUsedPrjAudAmt(object.getFlUsedPrjAudAmt());//融资二手车-已发起项目评审未通过的支付表敞口
			rzOldCar.setFlUsedPayAmt(object.getFlUsedPayAmt());//融资二手车-未放款支付表敞口
			rzOldCar.setFlUsedPaidAmt(object.getFlUsedPaidAmt());//融资二手车-已放款支付表敞口

			//担保新车
			CapPrjArInfoVO dbNewCar = new CapPrjArInfoVO();
			dbNewCar.setRecordType(0);
			dbNewCar.setRzOrDb(1);
			dbNewCar.setCarType(1);
			dbNewCar.setId(object.getId());
			dbNewCar.setCrtUsrId(object.getCrtUsrId());
			dbNewCar.setPrjId(object.getPrjId());//项目ID
			dbNewCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
			dbNewCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
			dbNewCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
			dbNewCar.setGtNewOvdHisCnt(object.getGtNewOvdHisCnt());//担保新车-历史30+逾期次数
			dbNewCar.setGtNewOvdCnt(object.getGtNewOvdCnt());//担保新车-当前逾期支付表数
			dbNewCar.setGtNewPrjAudAmt(object.getGtNewPrjAudAmt());//担保新车-已发起项目评审未通过的支付表敞口
			dbNewCar.setGtNewPayAmt(object.getGtNewPayAmt());//担保新车-未放款支付表敞口
			dbNewCar.setGtNewPaidAmt(object.getGtNewPaidAmt());//担保新车-已放款支付表敞口

			//担保二手车
			CapPrjArInfoVO dbOldCar = new CapPrjArInfoVO();
			dbOldCar.setRecordType(0);
			dbOldCar.setRzOrDb(1);
			dbOldCar.setCarType(0);
			dbOldCar.setId(object.getId());
			dbOldCar.setCrtUsrId(object.getCrtUsrId());
			dbOldCar.setPrjId(object.getPrjId());//项目ID
			dbOldCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
			dbOldCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
			dbOldCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
			dbOldCar.setGtUsedOvdHisCnt(object.getGtUsedOvdHisCnt());//担保二手车-历史30+逾期次数
			dbOldCar.setGtUsedOvdCnt(object.getGtUsedOvdCnt());//担保二手车-当前逾期支付表数
			dbOldCar.setGtUsedPrjAudAmt(object.getGtUsedPrjAudAmt());//担保二手车-已发起项目评审未通过的支付表敞口
			dbOldCar.setGtUsedPayAmt(object.getGtUsedPayAmt());//担保二手车-未放款支付表敞口
			dbOldCar.setGtUsedPaidAmt(object.getGtUsedPaidAmt());//担保二手车-已放款支付表敞口

			//汇总
			CapPrjArInfoVO total = new CapPrjArInfoVO();
			total.setRecordType(1);
			total.setRzOrDb(2);
			total.setCarType(2);
			total.setId(object.getId());
			total.setCrtUsrId(object.getCrtUsrId());
			total.setPrjId(object.getPrjId());//项目ID
			total.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
			total.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
			total.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
			total.setTotalOvdHisCnt(object.getFlNewOvdHisCnt() + object.getFlUsedOvdHisCnt() + object.getGtNewOvdHisCnt() + object.getGtUsedOvdHisCnt());
			total.setTotalOvdCnt(object.getFlNewOvdCnt() + object.getFlUsedOvdCnt() + object.getGtNewOvdCnt() + object.getGtUsedOvdCnt());
			total.setGtTotalPrjAudAmt(object.getFlNewPrjAudAmt() + object.getFlUsedPrjAudAmt() + object.getGtNewPrjAudAmt() + object.getGtUsedPrjAudAmt());
			total.setGtTotalPayAmt(object.getFlNewPayAmt() + object.getFlUsedPayAmt() + object.getGtNewPayAmt() + object.getGtUsedPayAmt());
			total.setGtTotalPaidAmt(object.getFlNewPaidAmt() + object.getFlUsedPaidAmt() + object.getGtNewPaidAmt() + object.getGtUsedPaidAmt());



			list.add(total);
			list.add(rzNewCar);
			list.add(rzOldCar);
			list.add(dbNewCar);
			list.add(dbOldCar);
		}

		return list;
	}


	/**
	 *
	 * @param prjId 项目ID
	 * @param mainId 主体ID
	 * @param mainName 主体名称
	 * @param mainTypCd 主体类型
	 * @return
	 */
	private fl.mongo.facade.capital.dto.CapPriArInfoDTO getMongoCapPriArInfoDTO(Long prjId,Long mainId,String mainNm,String mainTypCd){
		fl.mongo.facade.capital.dto.CapPriArInfoDTO dto = new fl.mongo.facade.capital.dto.CapPriArInfoDTO();
		dto.setId(mainId);
		dto.setPrjId(prjId);
		dto.setMainId(mainId);
		dto.setMainNm(mainNm);
		dto.setMainTypCd(mainTypCd);
		return dto;
	}

	/**
	 * 承租人ID
	 * @param prjId
	 * @return
	 */
	private List<Long> getCzCstIdList(Long prjId){
		List<Long> result = new ArrayList<>();
		//按类型分别查询 承租人 担人 挂靠公司 供应商
		//承租人
		List<PrjCstBscInfoVO> list = null;
		if(prjId!=null){
			PrjCstBscInfoQueryCondition cstBscInfoQc = new PrjCstBscInfoQueryCondition();
			cstBscInfoQc.setPrjId(prjId);
			cstBscInfoQc.setCstRlTyp(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1);
			list = PrjCstBscInfoVOTransform.toVOList(projectApiService.searchPrjCstBscInfoList(cstBscInfoQc,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd()));
			BaseLogUtils.info(logger,"getCzCstIdList","查询项目客户基本信息信息-->承租人,担保人,"+ JSON.toJSONString(list));
		}

		if(CollectionUtils.isNotEmpty(list)){
			for(PrjCstBscInfoVO prjCstBscInfoVO : list){
				if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(prjCstBscInfoVO.getCstRlCd())){
					//承租人
					if(!result.contains(prjCstBscInfoVO.getCstId())){
						result.add(prjCstBscInfoVO.getCstId());
					}
				}
			}
		}
		return result;
	}

	/**
	 * 担保人
	 * @param prjId
	 * @return
	 */
	private List<Long> getDbCstIdList(Long prjId) {
		List<Long> result = new ArrayList<>();
		List<PrjCstBscInfoVO> list = null;
		if(prjId!=null){
			PrjCstBscInfoQueryCondition cstBscInfoQc = new PrjCstBscInfoQueryCondition();
			cstBscInfoQc.setPrjId(prjId);
			cstBscInfoQc.setCstRlTyp(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2);
			list = PrjCstBscInfoVOTransform.toVOList(projectApiService.searchPrjCstBscInfoList(cstBscInfoQc,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd()));
			BaseLogUtils.info(logger,"getDbCstIdList","查询项目客户基本信息信息-->承租人,担保人,"+ JSON.toJSONString(list));
		}
		if(CollectionUtils.isNotEmpty(list)){
			for(PrjCstBscInfoVO prjCstBscInfoVO : list){
				if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2.equals(prjCstBscInfoVO.getCstRlCd())){
					//担保人
					if(!result.contains(prjCstBscInfoVO.getCstId())){
						result.add(prjCstBscInfoVO.getCstId());
					}
				}
			}
		}
		return result;
	}
	/**
	 * 挂靠公司ID
	 * @param prjId
	 * @return
	 */
	private List<Long> getLnkCompIdList(Long prjId) {
		List<Long> result = new ArrayList<>();
		List<PrjPrdDtlVO> list = null;
		if(prjId!=null){
			//挂靠公司
			PrjPrdDtlQueryCondition prjPrdDtlQc = new PrjPrdDtlQueryCondition();
			prjPrdDtlQc.setPrjId(prjId);
			prjPrdDtlQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			prjPrdDtlQc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			list = PrjPrdDtlVOTransform.toVOList(projectApiService.selectPrjPrdDtlList(prjPrdDtlQc));
			BaseLogUtils.info(logger,"getLnkCompIdList","查询项目产品明细信息-->挂靠公司,"+ JSON.toJSONString(list));
		}
		if(CollectionUtils.isNotEmpty(list)){
			//挂靠公司
			for(PrjPrdDtlVO prjPrdDtlVO : list){
				if(null != prjPrdDtlVO.getLnkCompId()){
					if(!result.contains(prjPrdDtlVO.getLnkCompId())){
						result.add(prjPrdDtlVO.getLnkCompId());
					}
				}
			}
		}
		return result;
	}

	/**
	 * 供应商ID
	 * @param prjId
	 * @return
	 */
	private List<Long> getSplIdList(Long prjId) {
		List<Long> result = new ArrayList<>();
		List<PrjPrdBscInfoVO> list = null;
		if(prjId!=null){
			//供应商信息
			PrjPrdBscInfoQueryCondition prjPrdBscInfoQc = new PrjPrdBscInfoQueryCondition();
			prjPrdBscInfoQc.setPrjId(prjId);
			list = PrjPrdBscInfoVOTransform.toVOList(this.projectApiService.searchPrjPrdList(prjPrdBscInfoQc,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd()));
			BaseLogUtils.info(logger,"getSplIdList","查询项目产品基本信息信息-->供应商,"+ JSON.toJSONString(list));
		}

		if(CollectionUtils.isNotEmpty(list)){
			//供应商
			for(PrjPrdBscInfoVO prjPrdBscInfoVO : list){
				if(null != prjPrdBscInfoVO.getSplId()){
					if(!result.contains(prjPrdBscInfoVO.getSplId())){
						result.add(prjPrdBscInfoVO.getSplId());
					}
				}
			}
		}
		return result;
	}

	@Override
	public List<CapPrjArInfoTableVO> selectTableList(Long prjId, String buOrgCd, String useCache) {
		List<CapPrjArInfoTableVO> resultList = new ArrayList<>();
		//承租人ID
		List<Long> czCstIdList = getCzCstIdList(prjId);
		//担保人ID
		List<Long> dbCstIdList = getDbCstIdList(prjId);
		//挂靠公司ID
		List<Long> lnkCompIdList = getLnkCompIdList(prjId);
		//供应商ID
		List<Long> splIdList = getSplIdList(prjId);
		Date date = new Date();
		List<CapPrjArInfoTableVO> czList = null;
		List<CapPrjArInfoTableVO> dbList = null;
		List<CapPrjArInfoTableVO> lnkList = null;
		List<CapPrjArInfoTableVO> splList = null;
		if("1".equals(useCache)){
			czList = selectTableListFromCache(czCstIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1, prjId,buOrgCd);
			dbList = selectTableListFromCache(dbCstIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2, prjId,buOrgCd);
			lnkList = selectTableListFromCache(lnkCompIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3, prjId,buOrgCd);
			splList = selectTableListFromCache(splIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4, prjId,buOrgCd);

		}else{
			czList= selectTableList(czCstIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1, date, buOrgCd);
			BaseLogUtils.newLogger(String.format("承租人数据 %s",JSON.toJSONString(czList))).info();
			dbList= selectTableList(dbCstIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1, date, buOrgCd);
			if(CollectionUtils.isNotEmpty(dbList)){
				for (CapPrjArInfoTableVO vo : dbList) {
					vo.setMainTypNm("担保人");// 无奈之举
				}
			}
			BaseLogUtils.newLogger(String.format("担保人数据 %s",JSON.toJSONString(dbList))).info();
			lnkList= selectTableList(lnkCompIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3, date, buOrgCd);
			BaseLogUtils.newLogger(String.format("挂靠公司数据 %s",JSON.toJSONString(lnkList))).info();
			splList = selectTableList(splIdList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4, date, buOrgCd);
			BaseLogUtils.newLogger(String.format("供应商数据 %s",JSON.toJSONString(splList))).info();
		}

		if(CollectionUtils.isNotEmpty(czList)){
            resultList.addAll(czList);
        }
        if(CollectionUtils.isNotEmpty(dbList)){
            resultList.addAll(dbList);
        }
        if(CollectionUtils.isNotEmpty(lnkList)){
            resultList.addAll(lnkList);
        }
        if(CollectionUtils.isNotEmpty(splList)){
            resultList.addAll(splList);
        }
		return resultList;
	}

	/**
	 * 从表中查数据
	 * @param idList
	 * @param codeMainTypCd1
	 * @param prjId
	 * @param buOrgCd
	 * @return
	 */
	private List<CapPrjArInfoTableVO> selectTableListFromCache(List<Long> idList, String codeMainTypCd1, Long prjId, String buOrgCd) {
		List<CapPrjArInfoDTO> list = null;
		if(CollectionUtils.isNotEmpty(idList)){
			for (Long id : idList) {
				CapPrjArInfoQC qc1 = new CapPrjArInfoQC();
				qc1.setPrjId(prjId);
				qc1.setMainId(id);
				qc1.setMainTypCd(codeMainTypCd1);
				List<String> orgCdList = new ArrayList<>();
				orgCdList.add("0000");
				if(StringUtils.isNotEmpty(buOrgCd)){
					orgCdList.add(buOrgCd);
				}
				qc1.setBuOrgCdList(orgCdList);
				list = this.capPrjArInfoApiService.searchList(qc1);
				if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3.equals(codeMainTypCd1)){
					qc1.setMainTypCd(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_5);
					List<CapPrjArInfoDTO> capPrjArInfoDTOS = this.capPrjArInfoApiService.searchList(qc1);
					if(CollectionUtils.isNotEmpty(capPrjArInfoDTOS)){
						if(list!=null){
							list.addAll(capPrjArInfoDTOS);
						}else{
							list=capPrjArInfoDTOS;
						}
					}
				}
			}
		}
		// 按照buorgCd拆分成2个list
		// buOrgCd=0000
		List<CapPrjArInfoDTO> list1 = new ArrayList<>();
		// buOrgCd=其他
		List<CapPrjArInfoDTO> list2 = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(list)){

			for (CapPrjArInfoDTO dto : list) {
				if("0000".equals(dto.getBuOrgCd())){
					list1.add(dto);
				}else{
					list2.add(dto);
				}
			}
		}
		if(CollectionUtils.isNotEmpty(list1)){
			return genTableRowFrom(CapPrjArInfoVOTransform.toVOList(list2),CapPrjArInfoVOTransform.toVOList(list1));
		}else{
			BaseLogUtils.newLogger("缓存数据查询失败返回空").info();
		}
		return null;
	}

	/**
     * 查询并封装数据
     * @param idList
     * @param codeMainTypCd1
     * @param date
     * @param buOrgCd
     */
    private List<CapPrjArInfoTableVO> selectTableList(List<Long> idList, String codeMainTypCd1, Date date, String buOrgCd) {
    	if(CollectionUtils.isEmpty(idList)){
    		return null;
		}
        List<CapPriArInfoDTO> list = null;
        if(StringUtils.isNotEmpty(buOrgCd) && !"0000".equals(buOrgCd)){
            BaseLogUtils.newLogger(String.format("查询承租人敞口数据 idList:%s,buOrgCd:%s,mainTypCd:%s",idList,buOrgCd,codeMainTypCd1)).info();
            list = this.capPriArInfoMongoService.getByListCondition(idList, codeMainTypCd1, date, buOrgCd);
            BaseLogUtils.newLogger(String.format("查询承租人敞口数据 结果:%s",JSON.toJSONString(list))).info();
        }
        BaseLogUtils.newLogger(String.format("查询承租人合计敞口数据 idList:%s,buOrgCd:0000,mainTypCd:%s",idList,buOrgCd,codeMainTypCd1)).info();
        List<CapPriArInfoDTO> allList = this.capPriArInfoMongoService.getByListCondition(idList, codeMainTypCd1, date, "0000");
        BaseLogUtils.newLogger(String.format("查询承租人合计敞口数据 结果:%s",JSON.toJSONString(allList))).info();

        // TODO
        if(codeMainTypCd1.equals(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3)){
			if(CollectionUtils.isEmpty(list)){
				list = new ArrayList<>();
			}
			if(CollectionUtils.isEmpty(allList)){
				allList = new ArrayList<>();
			}
			if(StringUtils.isNotEmpty(buOrgCd) && !"0000".equals(buOrgCd)){
				BaseLogUtils.newLogger(String.format("查询承租人敞口数据 idList:%s,buOrgCd:%s,mainTypCd:%s",idList,buOrgCd,CapPrjArInfoConstant.CODE_MAIN_TYP_CD_5)).info();
				List<CapPriArInfoDTO> byListCondition = this.capPriArInfoMongoService.getByListCondition(idList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_5, date, buOrgCd);
				if(CollectionUtils.isNotEmpty(byListCondition)){
					list.addAll(byListCondition);
				}
				BaseLogUtils.newLogger(String.format("查询承租人敞口数据 结果:%s",JSON.toJSONString(list))).info();
			}
			BaseLogUtils.newLogger(String.format("查询承租人合计敞口数据 idList:%s,buOrgCd:0000,mainTypCd:%s",idList,buOrgCd,CapPrjArInfoConstant.CODE_MAIN_TYP_CD_5)).info();
			List<CapPriArInfoDTO> byListCondition = this.capPriArInfoMongoService.getByListCondition(idList, CapPrjArInfoConstant.CODE_MAIN_TYP_CD_5, date, "0000");
			if(CollectionUtils.isNotEmpty(byListCondition)){
				allList.addAll(byListCondition);
			}
			BaseLogUtils.newLogger(String.format("查询承租人合计敞口数据 结果:%s",JSON.toJSONString(allList))).info();
		}
        // transform
        if(CollectionUtils.isNotEmpty(allList)){
            return genTableRowFrom(CapPrjArInfoVOTransform.mongoDtoVOList(list),CapPrjArInfoVOTransform.mongoDtoVOList(allList));
        }
        return null;
    }

    /**
	 * 生成TABLE VO
	 * @param arInfoDtoList
	 * @param allList
	 */
	private List<CapPrjArInfoTableVO> genTableRowFrom(List<CapPrjArInfoVO> arInfoDtoList, List<CapPrjArInfoVO> allList) {
		Map<String,CapPrjArInfoVO> map = new HashMap<>();
		if(CollectionUtils.isNotEmpty(arInfoDtoList)){
			for (CapPrjArInfoVO dto : arInfoDtoList) {
				map.put(dto.getMainId()+dto.getMainTypCd(),dto);
			}
		}
		List<CapPrjArInfoTableVO> result = new ArrayList<>();
		for (CapPrjArInfoVO sumDto : allList) {
			CapPrjArInfoVO dto = map.get(sumDto.getMainId() + sumDto.getMainTypCd());

			if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_5.equals(sumDto.getMainTypCd())){
				result.add(genResNewVO(dto, sumDto));
				result.add(genResOldVO(dto, sumDto));
			}else{
				result.add(genRzNewVO(dto, sumDto));
				result.add(genRzOldVO(dto, sumDto));
				result.add(genDbNewVO(dto, sumDto));
				result.add(genDbOldVO(dto, sumDto));
			}
        }
        if(CollectionUtils.isNotEmpty(result)){
			result.add(genSumVO(result));
		}

		return result;
	}

	/**
	 * 生成 挂靠公司提供货源敞口 行--新车
	 * @param dto
	 * @param sumDto
	 * @return
	 */
	private CapPrjArInfoTableVO genResNewVO(CapPrjArInfoVO dto, CapPrjArInfoVO sumDto) {
		CapPrjArInfoTableVO result = new CapPrjArInfoTableVO();

		result.setRzDb("提供货源敞口");
		result.setCarType("新车");
		if(dto!=null){
			result.setOvd30CountB(dto.getFlNewOvdHisCnt()==null?0L:dto.getFlNewOvdHisCnt());
			result.setOvdCountB(dto.getFlNewOvdCnt()==null?0L:dto.getFlNewOvdCnt());
			Long flNewPrjAudAmtB = dto.getFlNewPrjAudAmt();
			if(flNewPrjAudAmtB==null){
				flNewPrjAudAmtB=0L;
			}
			result.setPrjAudAmtB(flNewPrjAudAmtB);
			Long flNewPayAmtB = dto.getFlNewPayAmt();
			if(flNewPayAmtB ==null){
				flNewPayAmtB=0L;
			}
			result.setPayAmtB(flNewPayAmtB);
			Long flNewPaidAmtB = dto.getFlNewPaidAmt();
			if(flNewPaidAmtB==null){
				flNewPaidAmtB=0L;
			}
			result.setPaidAmtB(flNewPaidAmtB);
			result.setSumB(flNewPrjAudAmtB + flNewPayAmtB + flNewPaidAmtB);
		}
		if(sumDto !=null){
			result.setOvd30Count(sumDto.getFlNewOvdHisCnt()==null?0L:sumDto.getFlNewOvdHisCnt());
			result.setOvdCount(sumDto.getFlNewOvdCnt()==null?0L:sumDto.getFlNewOvdCnt());
			Long flNewPrjAudAmt = sumDto.getFlNewPrjAudAmt();
			if(flNewPrjAudAmt==null){
				flNewPrjAudAmt=0L;
			}
			result.setPrjAudAmt(flNewPrjAudAmt);
			Long flNewPayAmt = sumDto.getFlNewPayAmt();
			if(flNewPayAmt==null){
				flNewPayAmt=0L;
			}
			result.setPayAmt(flNewPayAmt);
			Long flNewPaidAmt = sumDto.getFlNewPaidAmt();
			if(flNewPaidAmt == null){
				flNewPaidAmt=0L;
			}
			result.setPaidAmt(flNewPaidAmt);
			result.setSum(flNewPrjAudAmt + flNewPayAmt + flNewPaidAmt );
		}
		return result;
	}
	private CapPrjArInfoTableVO genResOldVO(CapPrjArInfoVO dto, CapPrjArInfoVO sumDto) {
		CapPrjArInfoTableVO result = new CapPrjArInfoTableVO();

		result.setRzDb("提供货源敞口");
		result.setCarType("二手车");
		if(dto!=null){
			result.setOvd30CountB(dto.getFlUsedOvdHisCnt()==null?0L:dto.getFlUsedOvdHisCnt());
			result.setOvdCountB(dto.getFlUsedOvdCnt()==null?0L:dto.getFlUsedOvdCnt());
			Long prjAudAmtB = dto.getFlUsedPrjAudAmt();
			if(prjAudAmtB==null){
				prjAudAmtB=0L;
			}
			result.setPrjAudAmtB(prjAudAmtB);
			Long payAmtB = dto.getFlUsedPayAmt();
			if(payAmtB ==null){
				payAmtB=0L;
			}
			result.setPayAmtB(payAmtB);
			Long paidAmtB = dto.getFlUsedPaidAmt();
			if(paidAmtB==null){
				paidAmtB=0L;
			}
			result.setPaidAmtB(paidAmtB);
			result.setSumB(prjAudAmtB + payAmtB + paidAmtB);
		}
		if(sumDto !=null){
			result.setOvd30Count(sumDto.getFlUsedOvdHisCnt()==null?0L:sumDto.getFlUsedOvdHisCnt());
			result.setOvdCount(sumDto.getFlUsedOvdCnt()==null?0L:sumDto.getFlUsedOvdCnt());
			Long prjAudAmt = sumDto.getFlUsedPrjAudAmt();
			if(prjAudAmt==null){
				prjAudAmt=0L;
			}
			result.setPrjAudAmt(prjAudAmt);
			Long payAmt = sumDto.getFlUsedPayAmt();
			if(payAmt==null){
				payAmt=0L;
			}
			result.setPayAmt(payAmt);
			Long paidAmt = sumDto.getFlUsedPaidAmt();
			if(paidAmt == null){
				paidAmt=0L;
			}
			result.setPaidAmt(paidAmt);
			result.setSum(prjAudAmt + payAmt + paidAmt );
		}
		return result;
	}
	/**
     * 担保二手车
     * @param dto
     * @param sumDto
     * @return
     */
    private CapPrjArInfoTableVO genDbOldVO(CapPrjArInfoVO dto, CapPrjArInfoVO sumDto) {
        CapPrjArInfoTableVO result = new CapPrjArInfoTableVO();
        if(sumDto!=null){
            result.setMainNm(sumDto.getMainNm());
            String mainTypCd = sumDto.getMainTypCd();
            if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(mainTypCd)){
                result.setMainTypNm("承租人");
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2.equals(mainTypCd)){
                result.setMainTypNm("担保人");
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3.equals(mainTypCd)){
                result.setMainTypNm("挂靠公司");
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4.equals(mainTypCd)){
                result.setMainTypNm("供应商");
            }
        }
        result.setRzDb("担保");
        result.setCarType("二手车");
        if(dto!=null){
			result.setOvd30CountB(dto.getGtUsedOvdHisCnt()==null?0L:dto.getGtUsedOvdHisCnt());
			result.setOvdCountB(dto.getGtUsedOvdCnt()==null?0L:dto.getGtUsedOvdCnt());
			Long prjAudAmtB = dto.getGtUsedPrjAudAmt();
			if(prjAudAmtB==null){
				prjAudAmtB=0L;
			}
			result.setPrjAudAmtB(prjAudAmtB);
			Long payAmtB = dto.getGtUsedPayAmt();
			if(payAmtB ==null){
				payAmtB=0L;
			}
			result.setPayAmtB(payAmtB);
			Long paidAmtB = dto.getGtUsedPaidAmt();
			if(paidAmtB==null){
				paidAmtB=0L;
			}
			result.setPaidAmtB(paidAmtB);
			result.setSumB(prjAudAmtB + payAmtB + paidAmtB);
		}
        if(sumDto !=null){
            result.setOvd30Count(sumDto.getGtUsedOvdHisCnt()==null?0L:sumDto.getGtUsedOvdHisCnt());
            result.setOvdCount(sumDto.getGtUsedOvdCnt()==null?0L:sumDto.getGtUsedOvdCnt());
            Long prjAudAmt = sumDto.getGtUsedPrjAudAmt();
            if(prjAudAmt==null){
                prjAudAmt=0L;
            }
            result.setPrjAudAmt(prjAudAmt);
            Long payAmt = sumDto.getGtUsedPayAmt();
            if(payAmt==null){
                payAmt=0L;
            }
            result.setPayAmt(payAmt);
            Long paidAmt = sumDto.getGtUsedPaidAmt();
            if(paidAmt == null){
                paidAmt=0L;
            }
            result.setPaidAmt(paidAmt);
            result.setSum(prjAudAmt + payAmt + paidAmt );
        }
        return result;
    }

    /**
     * 担保新车
     * @param dto
     * @param sumDto
     * @return
     */
    private CapPrjArInfoTableVO genDbNewVO(CapPrjArInfoVO dto, CapPrjArInfoVO sumDto) {
        CapPrjArInfoTableVO result = new CapPrjArInfoTableVO();
        if(sumDto!=null){
            result.setMainNm(sumDto.getMainNm());
            String mainTypCd = sumDto.getMainTypCd();
            if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(mainTypCd)){
                result.setMainTypNm("承租人");
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2.equals(mainTypCd)){
                result.setMainTypNm("担保人");
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3.equals(mainTypCd)){
                result.setMainTypNm("挂靠公司");
            }else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4.equals(mainTypCd)){
                result.setMainTypNm("供应商");
            }
        }
        result.setRzDb("担保");
        result.setCarType("新车");
        if(dto!=null){
			result.setOvd30CountB(dto.getGtNewOvdHisCnt()==null?0L:dto.getGtNewOvdHisCnt());
			result.setOvdCountB(dto.getGtNewOvdCnt()==null?0L:dto.getGtNewOvdCnt());
			Long prjAudAmtB = dto.getGtNewPrjAudAmt();
			if(prjAudAmtB==null){
				prjAudAmtB=0L;
			}
			result.setPrjAudAmtB(prjAudAmtB);
			Long payAmtB = dto.getGtNewPayAmt();
			if(payAmtB ==null){
				payAmtB=0L;
			}
			result.setPayAmtB(payAmtB);
			Long paidAmtB = dto.getGtNewPaidAmt();
			if(paidAmtB==null){
				paidAmtB=0L;
			}
			result.setPaidAmtB(paidAmtB);
			result.setSumB(prjAudAmtB + payAmtB + paidAmtB);
		}

        if(sumDto !=null){
            result.setOvd30Count(sumDto.getGtNewOvdHisCnt()==null?0L:sumDto.getGtNewOvdHisCnt());
            result.setOvdCount(sumDto.getGtNewOvdCnt()==null?0L:sumDto.getGtNewOvdCnt());
            Long prjAudAmt = sumDto.getGtNewPrjAudAmt();
            if(prjAudAmt==null){
                prjAudAmt=0L;
            }
            result.setPrjAudAmt(prjAudAmt);
            Long payAmt = sumDto.getGtNewPayAmt();
            if(payAmt==null){
                payAmt=0L;
            }
            result.setPayAmt(payAmt);
            Long paidAmt = sumDto.getGtNewPaidAmt();
            if(paidAmt == null){
                paidAmt=0L;
            }
            result.setPaidAmt(paidAmt);
            result.setSum(prjAudAmt + payAmt + paidAmt );
        }
        return result;
    }
	/**
	 * 融资二手车
	 * @param dto
	 * @param sumDto
	 */
	private CapPrjArInfoTableVO genRzOldVO(CapPrjArInfoVO dto, CapPrjArInfoVO sumDto) {
		CapPrjArInfoTableVO result = new CapPrjArInfoTableVO();
		if(sumDto!=null){
			result.setMainNm(sumDto.getMainNm());
			String mainTypCd = sumDto.getMainTypCd();
			if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(mainTypCd)){
				result.setMainTypNm("承租人");
			}else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2.equals(mainTypCd)){
				result.setMainTypNm("担保人");
			}else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3.equals(mainTypCd)){
				result.setMainTypNm("挂靠公司");
			}else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4.equals(mainTypCd)){
				result.setMainTypNm("供应商");
			}
		}
		result.setRzDb("融资");
		result.setCarType("二手车");
		if(dto!=null){
			result.setOvd30CountB(dto.getFlUsedOvdHisCnt()==null?0L:dto.getFlUsedOvdHisCnt());
			result.setOvdCountB(dto.getFlUsedOvdCnt()==null?0L:dto.getFlUsedOvdCnt());
			Long prjAudAmtB = dto.getFlUsedPrjAudAmt();
			if(prjAudAmtB==null){
				prjAudAmtB=0L;
			}
			result.setPrjAudAmtB(prjAudAmtB);
			Long payAmtB = dto.getFlUsedPayAmt();
			if(payAmtB ==null){
				payAmtB=0L;
			}
			result.setPayAmtB(payAmtB);
			Long paidAmtB = dto.getFlUsedPaidAmt();
			if(paidAmtB==null){
				paidAmtB=0L;
			}
			result.setPaidAmtB(paidAmtB);
			result.setSumB(prjAudAmtB + payAmtB + paidAmtB);
		}
		if(sumDto !=null){
			result.setOvd30Count(sumDto.getFlUsedOvdHisCnt()==null?0L:sumDto.getFlUsedOvdHisCnt());
			result.setOvdCount(sumDto.getFlUsedOvdCnt()==null?0L:sumDto.getFlUsedOvdCnt());
			Long prjAudAmt = sumDto.getFlUsedPrjAudAmt();
			if(prjAudAmt==null){
				prjAudAmt=0L;
			}
			result.setPrjAudAmt(prjAudAmt);
			Long payAmt = sumDto.getFlUsedPayAmt();
			if(payAmt==null){
				payAmt=0L;
			}
			result.setPayAmt(payAmt);
			Long paidAmt = sumDto.getFlUsedPaidAmt();
			if(paidAmt == null){
				paidAmt=0L;
			}
			result.setPaidAmt(paidAmt);
			result.setSum(prjAudAmt + payAmt + paidAmt );
		}
		return result;
	}

	/**
	 * 获取融资新车数据
	 *
	 * @param dto
	 * @param sumDto
	 * @return
	 */
	private CapPrjArInfoTableVO genRzNewVO(CapPrjArInfoVO dto, CapPrjArInfoVO sumDto) {
		CapPrjArInfoTableVO result = new CapPrjArInfoTableVO();
		if(sumDto!=null){
			result.setMainNm(sumDto.getMainNm());
			String mainTypCd = sumDto.getMainTypCd();
			if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_1.equals(mainTypCd)){
				result.setMainTypNm("承租人");
			}else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_2.equals(mainTypCd)){
				result.setMainTypNm("担保人");
			}else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_3.equals(mainTypCd)){
				result.setMainTypNm("挂靠公司");
			}else if(CapPrjArInfoConstant.CODE_MAIN_TYP_CD_4.equals(mainTypCd)){
				result.setMainTypNm("供应商");
			}
		}
		result.setRzDb("融资");
		result.setCarType("新车");
		if(dto!=null){
			result.setOvd30CountB(dto.getFlNewOvdHisCnt()==null?0L:dto.getFlNewOvdHisCnt());
			result.setOvdCountB(dto.getFlNewOvdCnt()==null?0L:dto.getFlNewOvdCnt());
			Long flNewPrjAudAmtB = dto.getFlNewPrjAudAmt();
			if(flNewPrjAudAmtB==null){
				flNewPrjAudAmtB=0L;
			}
			result.setPrjAudAmtB(flNewPrjAudAmtB);
			Long flNewPayAmtB = dto.getFlNewPayAmt();
			if(flNewPayAmtB ==null){
				flNewPayAmtB=0L;
			}
			result.setPayAmtB(flNewPayAmtB);
			Long flNewPaidAmtB = dto.getFlNewPaidAmt();
			if(flNewPaidAmtB==null){
				flNewPaidAmtB=0L;
			}
			result.setPaidAmtB(flNewPaidAmtB);
			result.setSumB(flNewPrjAudAmtB + flNewPayAmtB + flNewPaidAmtB);
		}
		if(sumDto !=null){
			result.setOvd30Count(sumDto.getFlNewOvdHisCnt()==null?0L:sumDto.getFlNewOvdHisCnt());
			result.setOvdCount(sumDto.getFlNewOvdCnt()==null?0L:sumDto.getFlNewOvdCnt());
			Long flNewPrjAudAmt = sumDto.getFlNewPrjAudAmt();
			if(flNewPrjAudAmt==null){
				flNewPrjAudAmt=0L;
			}
			result.setPrjAudAmt(flNewPrjAudAmt);
			Long flNewPayAmt = sumDto.getFlNewPayAmt();
			if(flNewPayAmt==null){
				flNewPayAmt=0L;
			}
			result.setPayAmt(flNewPayAmt);
			Long flNewPaidAmt = sumDto.getFlNewPaidAmt();
			if(flNewPaidAmt == null){
				flNewPaidAmt=0L;
			}
			result.setPaidAmt(flNewPaidAmt);
			result.setSum(flNewPrjAudAmt + flNewPayAmt + flNewPaidAmt );
		}
		return result;
	}

	/**
	 * 生成合计 对象
	 * @param list
	 */
	private CapPrjArInfoTableVO genSumVO(List<CapPrjArInfoTableVO> list) {
		CapPrjArInfoTableVO result = null;
		if(CollectionUtils.isNotEmpty(list)){
			result = new CapPrjArInfoTableVO();
			result.setCarType("合计");
            for (CapPrjArInfoTableVO vo : list) {
                result.setOvd30Count(result.getOvd30Count()+vo.getOvd30Count());
                result.setOvd30CountB(result.getOvd30CountB() + vo.getOvd30CountB());
                result.setOvdCount(result.getOvdCount() + vo.getOvdCount());
                result.setOvdCountB(result.getOvdCountB() + vo.getOvdCountB());
                result.setPrjAudAmt(result.getPrjAudAmt() + vo.getPrjAudAmt());
                result.setPrjAudAmtB(result.getPrjAudAmtB() + vo.getPrjAudAmtB());
                result.setPayAmt(result.getPayAmt() + vo.getPayAmt());
                result.setPayAmtB(result.getPayAmtB() + vo.getPayAmtB());
                result.setPaidAmt(result.getPaidAmt() + vo.getPaidAmt());
                result.setPaidAmtB(result.getPaidAmtB() + vo.getPaidAmtB());
                result.setSum(result.getSum() + vo.getSum());
                result.setSumB(result.getSumB() + vo.getSumB());
            }
		}
		return result;
	}
}
